import csv
import json
import pprint

import requests
import re

def start():
	f = open('data.csv','a',encoding='utf-8',newline='')
	csv_writer = csv.DictWriter(f,fieldnames=[
		'店铺名称',
		'类型',
		'人均'
	])
	csv_writer.writeheader()
	for w in range(0,1600,32):
		#请求信息
		url = 'https://apimobile.meituan.com/group/v4/poi/pcsearch/1?uuid=1b79073647414959b763.1646094495.1.0.0&userid=2946395421&limit=32&offset='+str(w)+'&cateId=-1&q=%E7%81%AB%E9%94%85&token=k_Bqy39_q-yqJPwvcxapq_aYGIIAAAAAjBAAAINVx6mlSf-Frgfag_IaaeBEqOKnt6JlnGlsY8lMCXfJQcWg7HHiYRpEVxejTVfMNA'
		headers ={"Accept": "*/*", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6", "Connection": "keep-alive", "Cookie": "_lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; _lxsdk_cuid=17f42e0b524c8-07f56a515080c1-a3e3164-1fa400-17f42e0b524c8; mtcdn=K; _hc.v=a193fdd0-b0fd-9fef-29e7-e8b21376df71.1646094586; ci=1; rvct=1%2C70; uuid=7116d8b2b9a0487188c7.1646119245.1.0.0; WEBDFPID=5y2wy5639022583yz20w14x0ux707vw582u554z4zx297958uu0v0zu9-1646205647347; lt=ZWF4l4-y7Kj9Mi6A0rj_eGFs4k0AAAAAjBAAAJICCn-ihR49H5-a4QypM_L0JQpWPd-eUUtPlA2Y0XqtOz8RVZAnDjOCi1l36X4x5g; u=2946395421; n=vxl805079915; token2=ZWF4l4-y7Kj9Mi6A0rj_eGFs4k0AAAAAjBAAAJICCn-ihR49H5-a4QypM_L0JQpWPd-eUUtPlA2Y0XqtOz8RVZAnDjOCi1l36X4x5g; unc=vxl805079915; firstTime=1646121096647; _lxsdk_s=17f445a5973-f76-bc6-0b1%7C%7C100", "Host": "apimobile.meituan.com", "Origin": "https://bj.meituan.com", "Referer": "https://bj.meituan.com/", "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"98\", \"Google Chrome\";v=\"98\"", "sec-ch-ua-mobile": "?0", "sec-ch-ua-platform": "\"Windows\"", "Sec-Fetch-Dest": "empty", "Sec-Fetch-Mode": "cors", "Sec-Fetch-Site": "same-site", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36"}
		result = requests.get(url,headers=headers)
		print("写入成功")
		#表体
		searchresult = result.json()['data']['searchResult']
		for index in searchresult:
		   dict={
				"店铺名称":index['title'],
				"类型":index['backCateName'],
				"人均":index['avgprice'],
		}
		   csv_writer.writerow(dict)
		   print(dict)
if __name__ == '__main__':
    start()